package top.ljjapp.security.core.validate.code;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import top.ljjapp.security.core.properties.SecurityProperties;
import top.ljjapp.security.core.validate.code.image.ImageCodeGenerator;
import top.ljjapp.security.core.validate.code.sms.DefaultSmsCodeSender;
import top.ljjapp.security.core.validate.code.sms.SmsCodeSender;

/**
 * 验证码生成器配置
 */
@Configuration
public class ValidateCodeBeanConfig {

    @Autowired
    private SecurityProperties securityProperties;

    /**
     * 图形验证码生成(可自定义)
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(name = "imageValidateCodeGenerator")//当启动的时候没有这个bean的时候调用下面那个，否则调用新的
    public ValidateCodeGenerator imageValidateCodeGenerator() {
        ImageCodeGenerator codeGenerator = new ImageCodeGenerator();
        codeGenerator.setSecurityProperties(securityProperties);
        return codeGenerator;
    }

    /**
     * 短信验证码发送（可自定义）
     * @return
     */
    @Bean
    @ConditionalOnMissingBean(SmsCodeSender.class)//当启动的时候没有这个接口的实现类调用下面的，否则调用新的 第二种写法
    public SmsCodeSender smsCodeGenerator() {
        return new DefaultSmsCodeSender();
    }

}
